package com.xiaolin.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @ClassName FileController
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/10/6
 * @Version 1.0
 */
@Controller
@RequestMapping("/file")
public class FileController {

    /**
     * 文件上传
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request){
        String contentType = request.getContentType();
        InputStream inputStream =null;
        FileOutputStream outputStream=null;
        try {
            //1、获取文件输入流
            inputStream = file.getInputStream();
            //2、文件输出流
            outputStream=new FileOutputStream("D:\\1file\\hello.png");
            byte[] buffer=new byte[1024];
            int len=0;
            while ((len=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭资源遵循先打开的后关闭
            if(outputStream !=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream !=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "upload";
    }

    /**
     * 文件下载
     * @param request
     * @param response
     */
    @GetMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response){
        ServletContext servletContext = request.getServletContext();
        InputStream inputStream = null;
        try {
            //1、文件输入流
            inputStream = new FileInputStream("D:\\1file\\hello.png");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ServletOutputStream outputStream=null;
        //3、设置响应类型
        String mimeType = servletContext.getMimeType("D:\\1file\\hello.png");
        response.setContentType(mimeType);
        //4、设置响应头，表示要进行下载操作
        try {
            //在谷歌浏览器、IE浏览器、火狐浏览器等，文件名有中文要使用URL编码
            response.setHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode("哈哈.png","utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            //2、获取文件输出流
            outputStream = response.getOutputStream();
            byte[] buffer=new byte[1024];
            int len;
            while ((len=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
